﻿using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;

namespace HslCommunication.WebSocket
{
	/// <summary>
	/// WebSocket的问答机制的客户端，本客户端将会在请求头上追加 RequestAndAnswer: true，本客户端将会请求服务器的信息，然后等待服务器的返回<br />
	/// Client of WebSocket Q &amp; A mechanism, this client will append RequestAndAnswer: true to the request header, this client will request the server information, and then wait for the server to return
	/// </summary>
	// Token: 0x02000024 RID: 36
	public class WebSocketQANet : NetworkDoubleBase
	{
		/// <summary>
		/// 根据指定的ip地址及端口号，实例化一个默认的对象<br />
		/// Instantiates a default object based on the specified IP address and port number
		/// </summary>
		/// <param name="ipAddress">远程服务器的ip地址</param>
		/// <param name="port">端口号信息</param>
		// Token: 0x06000249 RID: 585 RVA: 0x00008F41 File Offset: 0x00007141
		public WebSocketQANet(string ipAddress, int port)
		{
			this.IpAddress = HslHelper.GetIpAddressFromInput(ipAddress);
			this.Port = port;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc />
		// Token: 0x0600024A RID: 586 RVA: 0x00008F6C File Offset: 0x0000716C
		protected override OperateResult InitializationOnConnect(Socket socket)
		{
			byte[] data = WebSocketHelper.BuildWsQARequest(this.IpAddress, this.Port);
			OperateResult operateResult = base.Send(socket, data);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.Receive(socket, -1, 10000, null);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = OperateResult.CreateSuccessResult();
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x0600024B RID: 587 RVA: 0x00008FD8 File Offset: 0x000071D8
		public override OperateResult<byte[]> ReadFromCoreServer(Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true)
		{
			ILogNet logNet = base.LogNet;
			if (logNet != null)
			{
				logNet.WriteDebug(this.ToString(), StringResources.Language.Send + " : " + SoftBasic.ByteToHexString(send, ' '));
			}
			OperateResult operateResult = base.Send(socket, send);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag2 = base.ReceiveTimeOut < 0;
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult<byte[]>(new byte[0]);
				}
				else
				{
					OperateResult<WebSocketMessage> operateResult2 = base.ReceiveWebSocketPayload(socket);
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
					}
					else
					{
						ILogNet logNet2 = base.LogNet;
						if (logNet2 != null)
						{
							logNet2.WriteDebug(this.ToString(), string.Format("{0} : OpCode[{1}] Mask[{2}] {3}", new object[]
							{
								StringResources.Language.Receive,
								operateResult2.Content.OpCode,
								operateResult2.Content.HasMask,
								SoftBasic.ByteToHexString(operateResult2.Content.Payload, ' ')
							}));
						}
						result = OperateResult.CreateSuccessResult<byte[]>(operateResult2.Content.Payload);
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x0600024C RID: 588 RVA: 0x00009104 File Offset: 0x00007304
		[DebuggerStepThrough]
		protected override Task<OperateResult> InitializationOnConnectAsync(Socket socket)
		{
			WebSocketQANet.<InitializationOnConnectAsync>d__3 <InitializationOnConnectAsync>d__ = new WebSocketQANet.<InitializationOnConnectAsync>d__3();
			<InitializationOnConnectAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<InitializationOnConnectAsync>d__.<>4__this = this;
			<InitializationOnConnectAsync>d__.socket = socket;
			<InitializationOnConnectAsync>d__.<>1__state = -1;
			<InitializationOnConnectAsync>d__.<>t__builder.Start<WebSocketQANet.<InitializationOnConnectAsync>d__3>(ref <InitializationOnConnectAsync>d__);
			return <InitializationOnConnectAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x0600024D RID: 589 RVA: 0x00009150 File Offset: 0x00007350
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadFromCoreServerAsync(Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true)
		{
			WebSocketQANet.<ReadFromCoreServerAsync>d__4 <ReadFromCoreServerAsync>d__ = new WebSocketQANet.<ReadFromCoreServerAsync>d__4();
			<ReadFromCoreServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadFromCoreServerAsync>d__.<>4__this = this;
			<ReadFromCoreServerAsync>d__.socket = socket;
			<ReadFromCoreServerAsync>d__.send = send;
			<ReadFromCoreServerAsync>d__.hasResponseData = hasResponseData;
			<ReadFromCoreServerAsync>d__.usePackHeader = usePackHeader;
			<ReadFromCoreServerAsync>d__.<>1__state = -1;
			<ReadFromCoreServerAsync>d__.<>t__builder.Start<WebSocketQANet.<ReadFromCoreServerAsync>d__4>(ref <ReadFromCoreServerAsync>d__);
			return <ReadFromCoreServerAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 和websocket的服务器交互，将负载数据发送到服务器端，然后等待接收服务器的数据<br />
		/// Interact with the websocket server, send the load data to the server, and then wait to receive data from the server
		/// </summary>
		/// <param name="payload">数据负载</param>
		/// <returns>返回的结果数据</returns>
		// Token: 0x0600024E RID: 590 RVA: 0x000091B1 File Offset: 0x000073B1
		public OperateResult<string> ReadFromServer(string payload)
		{
			return ByteTransformHelper.GetSuccessResultFromOther<string, byte[]>(base.ReadFromCoreServer(WebSocketHelper.WebScoketPackData(1, true, payload)), new Func<byte[], string>(Encoding.UTF8.GetString));
		}

		/// <inheritdoc cref="M:HslCommunication.WebSocket.WebSocketQANet.ReadFromServer(System.String)" />
		// Token: 0x0600024F RID: 591 RVA: 0x000091D8 File Offset: 0x000073D8
		[DebuggerStepThrough]
		public Task<OperateResult<string>> ReadFromServerAsync(string payload)
		{
			WebSocketQANet.<ReadFromServerAsync>d__6 <ReadFromServerAsync>d__ = new WebSocketQANet.<ReadFromServerAsync>d__6();
			<ReadFromServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
			<ReadFromServerAsync>d__.<>4__this = this;
			<ReadFromServerAsync>d__.payload = payload;
			<ReadFromServerAsync>d__.<>1__state = -1;
			<ReadFromServerAsync>d__.<>t__builder.Start<WebSocketQANet.<ReadFromServerAsync>d__6>(ref <ReadFromServerAsync>d__);
			return <ReadFromServerAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000250 RID: 592 RVA: 0x00009223 File Offset: 0x00007423
		public override string ToString()
		{
			return string.Format("WebSocketQANet[{0}:{1}]", this.IpAddress, this.Port);
		}
	}
}
